package com.shunhe.storage.ui.system;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.support.view.GridItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.HomeMenu;
import com.shunhe.storage.support.util.MVPConfig;
import com.shunhe.storage.ui.contact.CustomerListActivity;
import com.shunhe.storage.ui.goods.GoodsListActivity;
import com.shunhe.storage.ui.order_receive.ReceiveOrderInActivity;
import com.shunhe.storage.ui.order_receive.ReceiveOrderOutActivity;
import com.shunhe.storage.ui.repo.InOutRepoRecordActivity;
import com.shunhe.storage.ui.repo.InRepoOrderActivity;
import com.shunhe.storage.ui.repo.OutRepoOrderActivity;
import com.shunhe.storage.ui.repo.StockQueryActivity;
import com.shunhe.storage.ui.statis.StatisActivity;

import java.util.ArrayList;
import java.util.List;

// 首页
public class HomeActivity extends BaseActivity {
    private RecyclerView rcv_menu;// 首页菜单网格

    private HomeMenuAdapter adapter;
    private List<HomeMenu> menuList = new ArrayList<>();
    private static final String[] MENU_TITLE = {"入库下单", "出库下单", "出入库记录", "库存查询", "货品管理", "客户管理", "入库接单", "出库接单", "统计"};

    private View statusBarView;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_home;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_menu = findViewById(R.id.rcv_menu);

        GridLayoutManager manager = new GridLayoutManager(this, 3);
        adapter = new HomeMenuAdapter(this);
        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setHorizontalSpan(R.dimen.deco_1_dp)
                .setVerticalSpan(R.dimen.deco_1_dp)
                .setColorResource(R.color.divider_color)
                .setShowLastLine(false)
                .build();
        rcv_menu.addItemDecoration(divider);
        rcv_menu.setLayoutManager(manager);
        rcv_menu.setAdapter(adapter);

        for (int i = 0; i < 9; i++) {
            HomeMenu menu = new HomeMenu();
            menu.setName(MENU_TITLE[i]);
            menuList.add(menu);
        }
        adapter.setDataList(menuList);

        setListeners();

        setHeadTitle("顺和仓储");
        setRightText("登录");

        //延时加载数据.
//        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
//            @Override
//            public boolean queueIdle() {
//                if (isStatusBarLight()) {
//                    StatusBarUtil.setStatusBarLightMode(getWindow());
//                }
//                if (isStatusBar()) {
//                    initStatusBar();
//                    getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
//                        @Override
//                        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//                            initStatusBar();
//                        }
//                    });
//                }
////                mPresenter.initData();
//                return false;
//            }
//        });
    }

    private void initStatusBar() {
        if (statusBarView == null) {
            int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = getWindow().findViewById(identifier);
        }

        if (statusBarView != null) {
            if (isStatusBarLight()) {
                statusBarView.setBackgroundDrawable(null);
            }
            statusBarView.setBackgroundResource(MVPConfig.statusDrawable);
        }
    }

    //子类通过复写该方法,控制是否改变statusbar
    protected boolean isStatusBar() {
        return MVPConfig.isStatusBar();
    }

    //子类通过复写该方法,控制是否需要改变statusbar字体颜色
    protected boolean isStatusBarLight() {
        return MVPConfig.isStatusBarLight;
    }

    private void setListeners() {
        setOnClickListener();
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                if (position == 0) {// 入库下单
                    startActivity(InRepoOrderActivity.class);
                } else if (position == 1) {// 出库下单
                    startActivity(OutRepoOrderActivity.class);
                } else if (position == 2) {// 出入库记录
                    startActivity(InOutRepoRecordActivity.class);
                } else if (position == 3) {// 库存查询
                    startActivity(StockQueryActivity.class);
                } else if (position == 4) {// 货品管理
                    startActivity(GoodsListActivity.class);
                } else if (position == 5) {// 客户管理
                    startActivity(CustomerListActivity.class);
                } else if (position == 6) {// 入库接单
                    startActivity(ReceiveOrderInActivity.class);
                } else if (position == 7) {// 出库接单
                    startActivity(ReceiveOrderOutActivity.class);
                } else if (position == 8) {// 统计
                    startActivity(StatisActivity.class);
                }
            }
        });
    }

    @Override
    protected void onRightTextClick() {
        ToastUtils.showShort("登录");
        startActivity(LoginActivity.class);
    }

    private class HomeMenuAdapter extends ListBaseAdapter<HomeMenu> {

        public HomeMenuAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_home_menu_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            TextView tv_name = holder.getView(R.id.tv_name);

            ViewGroup.LayoutParams params = rl_item.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth() / 3;
            rl_item.setLayoutParams(params);

            HomeMenu menu = mDataList.get(position);
            tv_name.setText(menu.getName());
        }
    }
}